﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace zUpLoad.Common
{
    class cUpdate
    {

        public static string getdbVer(string dllName)
        {
            string cmdsql = "if(OBJECT_ID('ufsystem..zVer') is null) " +
                "create table ufsystem..zVer(dllname nvarchar(500),Ver nvarchar(50), fileImg image)";
            string rVer;
            using (SqlConnection cn = new SqlConnection(cSysStart.cConnstr))
            {
                SqlCommand cncmd = new SqlCommand(cmdsql, cn);
                try
                {
                    cn.Open();
                    cncmd.ExecuteNonQuery();
                    cmdsql = "select max(ver) from ufsystem..zVer where dllname=@dllname";
                    cncmd.CommandText = cmdsql;
                    cncmd.Parameters.Add(new SqlParameter("@dllname", dllName));
                    object o = cncmd.ExecuteScalar();
                    if (o == null || o == DBNull.Value)
                    {
                        rVer = "1900-01-01";
                    }
                    else
                    {
                        rVer = o.ToString();
                    }
                }
                catch (Exception ex)
                { throw new Exception(ex.Message); }
                finally
                { cn.Close(); }
            }
            return rVer;
        }
        public static void savefile2DB(string dllName, string VerName)
        {
            string filepath = System.Windows.Forms.Application.StartupPath;
            if (filepath.Substring(filepath.Length - 1, 1) != "\\")
            {
                filepath = filepath + "\\";
            }
            filepath = filepath + dllName;
            if (!System.IO.File.Exists(filepath)) { throw new Exception("没有找到" + filepath); }
            byte[] b = Common.cFile.getFileByte(dllName);

            string cmdsql = "delete from ufsystem..zVer where dllname=@dllname";
            using (SqlConnection cn = new SqlConnection(cSysStart.cConnstr))
            {
                SqlCommand cncmd = new SqlCommand(cmdsql, cn);
                try
                {
                    cn.Open();
                    cncmd.Parameters.Add("@dllname", SqlDbType.NVarChar, 500);
                    cncmd.Parameters["@dllname"].Value = dllName;
                    cncmd.ExecuteNonQuery();

                    cmdsql = "insert into ufsystem..zVer(dllname,Ver,fileImg)" +
                        "values(@dllname,@Ver,@fileImg)";
                    cncmd.CommandText = cmdsql;
                    cncmd.Parameters.Clear();
                    cncmd.Parameters.Add("@dllname", SqlDbType.NVarChar, 500);
                    cncmd.Parameters.Add("@Ver", SqlDbType.NVarChar, 50);
                    cncmd.Parameters.Add("@fileImg", SqlDbType.Image);

                    cncmd.Parameters["@dllname"].Value = dllName;
                    cncmd.Parameters["@Ver"].Value = VerName;
                    cncmd.Parameters["@fileImg"].Value = b;
                    cncmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                { throw new Exception(ex.Message); }
                finally
                { cn.Close(); }
            }
        }
        public static void getfileFromDB(string dllName)
        {

            SqlConnection cn = new SqlConnection(cSysStart.cConnstr);
            try
            {
                string cmdsql = "select top 1 fileImg from  ufsystem..zVer where dllname=@dllname";
                cn.Open();
                SqlCommand cncmd = new SqlCommand();
                cncmd.Connection = cn;
                cncmd.CommandText = cmdsql;
                cncmd.Parameters.Add(new SqlParameter("@dllname", dllName));
                object o = cncmd.ExecuteScalar();
                if (o == DBNull.Value || o == null)
                { throw new Exception(string.Format("没有获取到{0}的文件", dllName)); }
                byte[] b = (byte[])o;
                string filename = Application.StartupPath;
                filename += "\\" + dllName;
                Common.cFile.writefile(b, filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误，请重新登录用友，再次升级。如果还有问题，请与系统管理员联系！" + Environment.NewLine + ex.Message);
            }
            finally { cn.Close(); }
        }

        public static byte[] getDllFromDB(string dllName)
        {
            try
            {
                string cmdsql = "select top 1 fileImg from  ufsystem..zVer where dllname=@dllname";
                using (var cn = new SqlConnection(cSysStart.cConnstr))
                {
                    cn.Open();
                    var cncmd = cn.CreateCommand();
                    cncmd.CommandText = cmdsql;
                    cncmd.Parameters.Add(new SqlParameter("@dllname", dllName));
                    object o = cncmd.ExecuteScalar();
                    if (o == DBNull.Value || o == null)
                    { MessageBox.Show(string.Format("没有获取到{0}的文件", dllName)); return null; }
                    byte[] b = (byte[])o;
                    return b;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误，请重新登录用友，再次升级。如果还有问题，请与系统管理员联系！" + Environment.NewLine + ex.Message);
                return null;
            }
        }

    }
}
